package jlib.component.clock
{
	import jlib.utils.DateUtil;
	/**
	 * TimeObject Class
	 * Native의 Date를 우리나라 실정에 맞는 시간과 편의를 제공해준다. 
	 * @author park jong hee
	 */
	public class TimeObject
	{
		private var year:int;
		private var month:int;
		private var date:int;
		private var hour:int;
		private var min:int;
		private var sec:int;
		
		/**
		 * TimeObject의 생성자. 시간을 설정해 생성합니다. 
		 * @param $year 년도
		 * @param $month 월
		 * @param $date 일
		 * @param $hour 시간
		 * @param $min 분
		 * @param $sec 초
		 * 
		 */		
		public function TimeObject($year:int=-1,$month:int=-1,$date:int=-1,$hour:int=-1,$min:int=-1,$sec:int=-1)
		{
			var hasNonSetting:Boolean = ($year + $month + $date + $hour + $min + $sec <= 0);
			
			if(hasNonSetting) {
				setDate(new Date());
			}
			
		}
		/**
		 * 시간을 설정합니다.
		 * @param $date Date 객체.
		 */		
		public function setDate($date:Date): void
		{
			setProperty($date.getFullYear(),$date.getMonth(),int($date.getDate()),$date.getHours(),$date.getMinutes(),$date.getSeconds());
		}
		
		private function setProperty($year:int,$month:int,$date:int,$hour:int,$min:int,$sec:int): void
		{
			year = $year;
			month = $month;
			date = $date;
			hour = $hour;
			min = $min;
			sec = $sec;
		}
		
		/**
		 * 현재 설정된 시간에 분을 추가합니다. 
		 * @param $min 분
		 */		
		public function addMin($min:int): void 
		{ 
			setDate(DateUtil.addMinutes(getNativeDate(),$min));
		}
		
		/**
		 * 현재 설정된 시간에 일을 추가합니다. 
		 * @param $days 일
		 */		
		public function addDays($days:int): void 
		{ 
			setDate(DateUtil.addDays(getNativeDate(),$days));
		}
		
		/**
		 * 현재 설정된 시간에 주를 추가합니다. 
		 * @param $weeks 월
		 */		
		public function addWeeks($weeks:int): void
		{
			setDate(DateUtil.addWeeks(getNativeDate(),$weeks));
		}
		
		public function getSec():int { return sec; }
		public function getMin():int { return min; }
		public function getHour():int { return hour; }
		public function getDate():int { return date; }
		public function getDay():int { return getNativeDate().getDay(); }
		public function getMonth():int { return month; }
		public function getYear():int { return year; }
		public function getNativeDate():Date { return new Date(getYear(),getMonth(),getDate(),getHour(),getMin(),getSec()); }
		
		public function getAMPM(mode:String='US'):String 
		{
			var modifier:String;
			switch(mode){
				case 'US': modifier = "PM";break;
				case 'KR': modifier = '오후'; break;
			}
			var hrs:int = getHour();
			if(hrs == 0){
				switch(mode){
					case 'US': modifier = "AM";break;
					case 'KR': modifier = '오전'; break;
				}
			} else if(hrs < 12){
				switch(mode){
					case 'US': modifier = "AM";break;
					case 'KR': modifier = '오전'; break;
				}
			} 
			return modifier;
		}
		
		public function getKoreanDay():String 
		{
			var day:Number = getNativeDate().getDay();
			var result:String;
			switch(day){
				case 0: result = '일'; break;
				case 1: result = '월'; break;
				case 2: result = '화'; break;
				case 3: result = '수'; break;
				case 4: result = '목'; break;
				case 5: result = '금'; break;
				case 6: result = '토'; break;
			}
			return result;
		}
		
		/**
		 * (토|일) 주말인지 아닌지를 판별합니다.
		 * @return boolean  
		 */		
		public function isWeekend(): Boolean 
		{
			var day:Number = getNativeDate().getDay();
			var result:Boolean = false;
			if(day == 0 || day == 6) result = true;
			return result;
		}
		
		/**
		 * 동일한 시간인지 검사합니다. 
		 * @param timeObject 시간객체
		 * @return boolean
		 */		
		public function equals(timeObject:TimeObject): Boolean
		{
			var isYearTrue:Boolean = timeObject.getYear()==this.getYear();
			var isMonthTrue:Boolean = timeObject.getMonth()==this.getMonth();
			var isDayTrue:Boolean = timeObject.getDate()==this.getDate();
			var isTimeTrue:Boolean = timeObject.getUSClockTime()==this.getUSClockTime();
			return (isYearTrue&&isMonthTrue&&isDayTrue&&isTimeTrue);	
		}
		
		public function getSimpleDate($division:String): String
		{
			return getYear()+$division+doubleDigitFormat(getMonth()+1)+$division+doubleDigitFormat(getDate());
		}
		
		public function getKoreanDate(): String 
		{
			return getYear()+'년 '+int(getMonth()+1)+'월 '+getDate()+'일 '+getKoreanDay()+'요일';
		}
		
		public function getKoreanFullTime(): String
		{
			return  getKoreanDate()+' '+getKoreanClockTime();
		}
		
		public function getKoreanClockTime(): String
		{
			var minLabel:String = doubleDigitFormat(getMin());
			var hrs:int = getHour();
			if(hrs > 12) {
				hrs = hrs-12;
			} else if(hrs == 0) {
				hrs = 12;
			}
			return (getAMPM('KR')+" "+doubleDigitFormat(hrs) + ":" + minLabel);
		}
		
		public function getUSClockTime(): String 
		{
			var minLabel:String = doubleDigitFormat(getMin());
			var hrs:int = getHour();
			if(hrs > 12) {
				hrs = hrs-12;
			} else if(hrs == 0) {
				hrs = 12;
			}
			return (getAMPM()+" "+doubleDigitFormat(hrs) + ":" + minLabel);
		}
		
		public function clone(): TimeObject 
		{
			var clone:TimeObject = new TimeObject();
			clone.setDate(this.getNativeDate());
			return 	clone;
		}
		
		private function doubleDigitFormat(num:uint):String 
		{
			if(num < 10) return ("0" + num);
			return num.toString();
		}
		
	}//c
}